// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const query = event.query
  const regNum = /^\d+$/
  const booksDb = cloud.database().collection('books')
  // 如果是纯数字
  if (regNum.test(query)) {
    const bookId = parseInt(query)
    const bookData = await booksDb.where({
      bookId
    }).get()
    const book = bookData.data[0]
    return book
  } else {
    // 如果是普通字符串
    const booksData = await booksDb.where({
      bookName: cloud.database().RegExp({
        regexp: `.*${query}`,
        options: 'i'
      })
    }).get()
    const books = booksData.data
    return books
  }
}